#region (c) 2014 THN Solutions LLC. - All rights reserved
/*
Simplified BSD License (BSD)
Copyright (c) 2014, THN Solutions LLC. ( www.ThnSolutions.com )
Author: Nguyễn, M. Hải                 ( www.Minh-Hai.com     )
All rights reserved.

Redistribution and use in source and binary forms, with or without modification,
are permitted provided that the following conditions are met:

    * Redistributions of source code must retain the above copyright notice, this 
      list of conditions and the following disclaimer.

    * Redistributions in binary form must reproduce the above copyright notice, 
      this list of conditions and the following disclaimer in the documentation 
      and/or other materials provided with the distribution.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND 
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 
IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, 
BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 
POSSIBILITY OF SUCH DAMAGE.
*/
#endregion

#region Record of Change
//             							CHANGE HISTORY
// -------------------------------------------------------------------------------------
// |   DATE    | DEVELOPER  | DESCRIPTION                                              |
// -------------------------------------------------------------------------------------
// | 13-Nov-09 | HaiNM      | First creation.                                          |
// -------------------------------------------------------------------------------------
//
#endregion

#region Record of Audit
//             							REVIEW HISTORY
// -------------------------------------------------------------------------------------
// |   DATE    | REVIEWER   | NOTES                                                    |
// -------------------------------------------------------------------------------------
// -------------------------------------------------------------------------------------
//
#endregion

#region Using directives
using System;
using System.Drawing;
using System.Windows.Forms;
using System.ComponentModel;
using System.Collections;

using Thn;
using Thn.Interface.Vcl;

using Demo;
#endregion

namespace PageDemo
{
    /// <summary>
    /// 
    /// </summary>
#if DEBUG 
    public
#else
    public
#endif 
    partial class fmMain : Form
    {
        #region Fields
        #endregion

        #region Properties
        #endregion

        #region Toggle Controls
        /// <summary>
        /// Toggle various controls based on current states.
        /// </summary>
        void ToggleControls()
        {
            btnBack.Enabled = pageMan.CanBack;
            btnForward.Enabled = pageMan.CanForward;

            //btnBack.Visible = pageMan.CanBack;
            //btnForward.Visible = pageMan.CanForward;
        }
        #endregion

        #region UI Events processing

        #region btnHome_Click
        private void btnHome_Click(object sender, EventArgs e)
        {
            pageMan.Show("Home");
        }
        #endregion

        #region btnBack_Click
        private void btnBack_Click(object sender, EventArgs e)
        {
            pageMan.Back();
        }
        #endregion

        #region btnForward_Click
        private void btnForward_Click(object sender, EventArgs e)
        {
            pageMan.Forward();
        }
        #endregion

        #region pageMan_PageShown
        private void pageMan_PageShown(IPageManager manager, PageShowContext context)
        {
            Log.Debug("Page shown - {0}", context.Descriptor);
            ToggleControls();
        }
        #endregion

        #region pageMan_PageHidden
        private void pageMan_PageHidden(IPageManager manager, IPageDescriptor page)
        {
            Log.Debug("Page hidden - {0}", page);
        }
        #endregion

        #endregion

        #region Initialize Page System
        void InitializePageSystem()
        {
            #region Register page types
            /*
             * We register the page types so that the page manager
             * can look for the correct types when it load page map file.
             * If we forget to register, an exception of "Cannot resolve type [xxxx]
             * shall be raised.
             */

            TypeResolver.Register(typeof(HomePage));
            TypeResolver.Register(typeof(EditPage));
            TypeResolver.Register(typeof(LoginPage));
            TypeResolver.Register(typeof(DummyPage));
            TypeResolver.Register(typeof(DummyChild1));
            TypeResolver.Register(typeof(DummyChild2));
            TypeResolver.Register(typeof(ManyControls1));
            TypeResolver.Register(typeof(ManyControls2));
            TypeResolver.Register(typeof(AdminOnlyPage));
            TypeResolver.Register(typeof(UnauthenticatedPage));
            TypeResolver.Register(typeof(UnauthorizedPage));
            #endregion

            string filename = "PageMap.xml";
            pageMan.LoadMap(filename);
        }
        #endregion

        #region On Load
        /// <summary>
        /// Initialize this form when form is loaded.
        /// </summary>
        protected override void OnLoad(EventArgs e)
        {
            if (!DesignMode)
            {
                InitializePageSystem();
                ToggleControls();

                //show default page
                pageMan.Show("Home");
                //pageMan.Show("Login");
                //pageMan.Show("AdminOnly");
            }
            #region Defensive tracing
            else
            {
                //don't do anything at design-time
            }
            #endregion

            //let my ancestor process its own logic
            base.OnLoad(e);
        }
        #endregion
        
        #region Constructors
        /// <summary>
        /// Default constructor.
        /// </summary>
        public fmMain() 
        {
            InitializeComponent();            
        }
        #endregion
    }
}
